
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "print_data.h"
#include "protocol.h"

void print_A0(unsigned char *frame_data, unsigned char len){}

void print_92(MYSQL *mysql, int id, unsigned char *frame_data, unsigned char len)
{
  int n = 0;
	char *command_arg=(char *)malloc(255);
	
	bzero(command_arg,255);

  printf("SOF:        %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;

	sprintf(command_arg, 
		"update rawdata set \
			macid='%02X%02X%02X',\
			data_type='%02X',\
			len='%02X',\
			message_number='%02X',\
			through_mode='%02X',\
			car_number='%02X',\
			first_time='%02X',\
			occupation_time='%02x',\
			cycle='%02X',\
			where id=%u", 
		frame_data[2], 
		frame_data[3], 
		frame_data[4], 
		frame_data[5], 
		frame_data[6], 
		frame_data[9], 
		frame_data[10], 
		frame_data[11], 
		frame_data[12], 
		id);
	
	mysql_real_query(mysql,command_arg,strlen(command_arg));

  printf("MAC_ID:     %02X %02X %02X\n", frame_data[n], frame_data[n + 1], frame_data[n + 2]);
  n += 3;

  printf("Data Type:  %02X\n", frame_data[n]);
  n += 1;

  printf("Data Len:   %02X\n", frame_data[n]);
  n += 1;

  printf("Message Number:     %02X\n", frame_data[n]);
  n += 1;

  printf("Through Mode:    		%02X\n", frame_data[n]);
  n += 1;

  printf("Car number: %02X\n", frame_data[n]);
  n += 1;

  printf("First Time: %02X\n", frame_data[n]);
  n += 1;

  printf("Occupation Time:    	%02X\n", frame_data[n]);
  n += 1;

  printf("Cycle: 			%02X\n", frame_data[n]);

	free(command_arg);
  free(frame_data);
}

void print_C0(unsigned char *frame_data, unsigned char len){}

void print_C4(MYSQL *mysql, int id, unsigned char *frame_data, unsigned char len)
{
  int n = 0;
  int loop;
	char *tag;
	char *command_arg=(char *)malloc(255);
	
	bzero(command_arg,255);

  printf("SOF:        %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;

	sprintf(command_arg, "update rawdata set macid='%02X%02X',uk='%02X%02X',len='%02X',data_type='%02X',node_id='%02X%02X%02X' where id=%u", frame_data[2], frame_data[3], frame_data[4], frame_data[5], frame_data[6], frame_data[9], frame_data[10], frame_data[11], frame_data[12], id);
  printf("MAC_ID:     %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;
	mysql_real_query(mysql,command_arg,strlen(command_arg));

  printf("Uk:         %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;

  printf("Data Len:   %02X\n", frame_data[n]);
  n += 1;

  printf("Check:      %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;

  printf("Data Type:  %02X\n", frame_data[n]);
  n += 1;

	printf("NODE ID:    %02X%02X%02X\n", frame_data[n], frame_data[n + 1], frame_data[n + 2]);
	n += 3;

	sprintf(command_arg, "update rawdata set car_status=%02X,jam_phase=%02X,message_number=%02X where id=%u", frame_data[n], frame_data[n + 1], frame_data[n + 5], id);
	mysql_real_query(mysql,command_arg,strlen(command_arg));
	printf("Node Status:	%02X\n", frame_data[n]);
	n += 1;

	printf("Jam Phase:		%02X\n", frame_data[n]);
	n += 4;

  printf("Message Number: %02X\n", frame_data[n]);

	protocol_C4(frame_data);

	free(command_arg);
  free(frame_data);
	
}

void print_C6(MYSQL *mysql, int id, unsigned char *frame_data, unsigned char len)
{
  int n = 0;
  int loop;
	char *tag;
	char *command_arg=(char *)malloc(255);
	
	bzero(command_arg,255);

  printf("SOF:        %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;

	sprintf(command_arg, "update rawdata set macid='%02X%02X',uk='%02X%02X',len='%02X',data_type='%02X',node_id='%02X%02X%02X' where id=%u", frame_data[2], frame_data[3], frame_data[4], frame_data[5], frame_data[6], frame_data[9], frame_data[10], frame_data[11], frame_data[12], id);
  printf("MAC_ID:     %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;
	mysql_real_query(mysql,command_arg,strlen(command_arg));

  printf("Uk:         %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;

  printf("Data Len:   %02X\n", frame_data[n]);
  n += 1;

  printf("Check:      %02X %02X\n", frame_data[n], frame_data[n + 1]);
  n += 2;

  printf("Data Type:  %02X\n", frame_data[n]);
  n += 1;

  printf("NODE ID:    %02X%02X%02X\n", frame_data[n], frame_data[n + 1], frame_data[n + 2]);
  n += 3;

	for (loop = 1; loop < (frame_data[6] - 1) / 4; loop++)
	{
		switch (loop)
		{
			case 1:
				tag = "first";
				break;
			case 2:
				tag = "secend";
				break;
			case 3:
				tag = "third";
				break;
		}			
		sprintf(command_arg, "update rawdata set %s_interval_time=%u,%s_through_time=%u where id=%u", tag, swap(frame_data[n], frame_data[n + 1]), tag, swap(frame_data[n+2], frame_data[n + 3]), id);
		mysql_real_query(mysql,command_arg,strlen(command_arg));
		printf("No.%2d interval time:  %4u ms\n", loop, swap(frame_data[n], frame_data[n + 1]));
		n += 2;

//		sprintf(command_arg, "update rawdata set %s_through_time=%u where id=%u", tag, swap(frame_data[n], frame_data[n + 1]), id);
//		mysql_real_query(mysql,command_arg,strlen(command_arg));
		printf("No.%2d through time:   %4u ms\n", loop, swap(frame_data[n], frame_data[n + 1]));
		n += 2;

	}

	sprintf(command_arg, "update rawdata set message_number='%02X' where id=%u", frame_data[n], id);
  printf("Message ID: %02X\n", frame_data[n]);
	mysql_real_query(mysql,command_arg,strlen(command_arg));

	free(command_arg);
  free(frame_data);
}

void print_C7(unsigned char *frame_data, unsigned char len){}

void print_CC(unsigned char *frame_data, unsigned char len){}

void print_DD(unsigned char *frame_data, unsigned char len){}

void print_D1(unsigned char *frame_data, unsigned char len){}

void print_D2(unsigned char *frame_data, unsigned char len){}

void print_FF(unsigned char *frame_data, unsigned char len){}

void print_C15(unsigned char *frame_data, unsigned char len){}

void print_C14(unsigned char *frame_data, unsigned char len){}
